package com.das.mechanic_base.widget;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.camera.X3TagDetectionAdapter;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.camera.TagDetectionBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3EdittextWatcher;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.kproduce.roundcorners.RoundView;

import java.util.List;

import razerdp.basepopup.BasePopupWindow;
import razerdp.util.animation.AlphaConfig;
import razerdp.util.animation.AnimationHelper;

public class X3BottomSelectTagDialog extends BasePopupWindow implements View.OnClickListener, X3TagDetectionAdapter.IOnClickItem {

    private RecyclerView rlv_item;
    private LinearLayout ll_top;
    private RoundView v_top;
    private EditText et_input;
    private ImageView iv_empty;
    private X3SwipeBackLayout sl_view;
    private TextView tv_affirm;
    private X3TagDetectionAdapter tagAdapter;
    private Context mContext;
    private Handler mHandler = new Handler();

    public X3BottomSelectTagDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.x3_bottom_select_tag_dialog);
    }

    @Override
    public void onViewCreated(@NonNull View contentView) {
        super.onViewCreated(contentView);

        rlv_item = findViewById(R.id.rlv_item);
        ll_top = findViewById(R.id.ll_top);
        v_top = findViewById(R.id.v_top);
        et_input = findViewById(R.id.et_input);
        iv_empty = findViewById(R.id.iv_empty);
        tv_affirm = findViewById(R.id.tv_affirm);
        sl_view = findViewById(R.id.sl_view);

        rlv_item.setLayoutManager(new LinearLayoutManager(contentView.getContext()));
        tagAdapter = new X3TagDetectionAdapter(contentView.getContext());
        rlv_item.setAdapter(tagAdapter);
        tagAdapter.setiOnClickItem(this);

        et_input.addTextChangedListener(new X3EdittextWatcher() {
            @Override
            protected void textInputAfter(String content) {
                iv_empty.setVisibility(X3StringUtils.isEmpty(content) ? View.GONE : View.VISIBLE);
                tv_affirm.setVisibility(X3StringUtils.isEmpty(content) ? View.GONE : View.VISIBLE);
            }
        });

        iv_empty.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);

        sl_view.setIsBackToEnd(false);
        sl_view.setSwipeBackListener(new X3SwipeBackLayout.OnSwipeBackListener() {
            @Override
            public void onViewPositionChanged(View mView, float swipeBackFraction, float swipeBackFactor) {
                if (swipeBackFraction >= 0.5) {
                    onViewSwipeFinished(mView,true);
                }
            }

            @Override
            public void onViewSwipeFinished(View mView, boolean isEnd) {
                if (isEnd) {
                    dismiss();
                }
            }

            @Override
            public void onViewBack() {
                // 下拉弹回
            }
        });
    }

    public void setTagDetectionId(long itemId) {
        NetWorkHttp.getApi().getQuestionBaseListByTempletItemId(itemId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<List<TagDetectionBean>>() {
                    @Override
                    protected void onDone(List<TagDetectionBean> tagDetectionBeans) {
                        if (X3StringUtils.isListEmpty(tagDetectionBeans)) {
                            v_top.setVisibility(View.GONE);
                            rlv_item.setVisibility(View.GONE);
                            ll_top.setVisibility(View.GONE);
                            X3StringUtils.showSoftInput(et_input);
                            return;
                        }
                        v_top.setVisibility(View.VISIBLE);
                        rlv_item.setVisibility(View.VISIBLE);
                        ll_top.setVisibility(View.VISIBLE);
                        if (tagAdapter != null) {
                            tagAdapter.changeTagData(tagDetectionBeans);
                        }

                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_empty) {
            et_input.setText("");
        } else if (id == R.id.tv_affirm) {
            String tag = et_input.getText().toString().trim();
            if (X3StringUtils.isEmpty(tag)) {
                X3ToastUtils.showMessage(mContext.getString(R.string.x3_no_input_trim));
                return;
            }
            if (iOnSelectTag != null) {
                iOnSelectTag.iOnSelectTag(tag);
            }
            dismiss();
        }
    }

    IOnSelectTag iOnSelectTag;

    public X3BottomSelectTagDialog setiOnSelectTag(IOnSelectTag iOnSelectTag) {
        this.iOnSelectTag = iOnSelectTag;
        return this;
    }

    public interface IOnSelectTag {
        void iOnSelectTag(String content);
    }

    @Override
    public void iOnClickItem(String content) {
        if (iOnSelectTag != null) {
            iOnSelectTag.iOnSelectTag(content);
        }
        dismiss();
    }

    @Override
    public void showPopupWindow() {
        super.showPopupWindow();
        iv_empty.performClick();
        et_input.setFocusable(true);
        et_input.setFocusableInTouchMode(true);
        et_input.requestFocus();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) et_input.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(et_input, 0);
            }
        },200);
    }

    @Override
    protected Animation onCreateShowAnimation() {
        return AnimationHelper.asAnimation()
                .withAlpha(AlphaConfig.IN.duration(0))
                .toShow();
    }

    @Override
    public void dismiss() {
        X3StringUtils.hideSoftInput(et_input);
        super.dismiss();
    }

    @Override
    protected Animation onCreateDismissAnimation() {
        return AnimationHelper.asAnimation()
                .withAlpha(AlphaConfig.OUT.duration(0))
                .toDismiss();
    }
}
